package com.spark.lola.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.entity.gds.ProductInfo;
import com.spark.lola.entity.sup.SupplierInfo;
import com.spark.lola.feign.LogisticsFeignService;
import com.spark.lola.mapper.gds.ProductInfoMapper;
import com.spark.lola.mapper.sup.SupplierInfoMapper;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.wms.cache.CollectionCache;
import com.spark.lola.support.wms.constant.KafkaTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: DengYeJun
 * @Date: 2020/7/3 17:27
 */
@RequestMapping("/")
@RestController
public class RepiarController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Autowired
    private SupplierInfoMapper supplierInfoMapper;
    @Autowired
    private LogisticsFeignService logisticsFeignService;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @RequestMapping("product")
    public ResponseBean product(@RequestParam(value = "id",defaultValue = "0") Long productId,@RequestParam("date") String date) throws InterruptedException {
        List<Long> ids =new ArrayList<>();
        if(productId>0){
            ids.add(productId);
        }else{
            QueryWrapper<ProductInfo> queryWrapper =new QueryWrapper<>();
            queryWrapper.select("id");
            queryWrapper.eq("used", CommonConstant.Y);
            List<ProductInfo> lst = productInfoMapper.selectList(queryWrapper);
            lst.forEach(o->ids.add(o.getId()));
        }
        for(Long id:ids){
            logger.info("===修复货品【{}】日期【{}】",id,date);
            kafkaTemplate.send(KafkaTopic.STATISTICS_CAL_PRODCUT,id.toString(),date);
            Thread.sleep(2*1000);
        }
        return ResponseBean.success();
    }

    @RequestMapping("supplier")
    public ResponseBean supplier(@RequestParam(value = "id",defaultValue = "0") Long productId,@RequestParam("date") String date) throws InterruptedException {
        List<Long> ids =new ArrayList<>();
        if(productId>0){
            ids.add(productId);
        }else{
            QueryWrapper<SupplierInfo> queryWrapper =new QueryWrapper<>();
            queryWrapper.select("id");
            queryWrapper.eq("used", CommonConstant.Y);
            // queryWrapper.gt("order_count",0);
            List<SupplierInfo> lst = supplierInfoMapper.selectList(queryWrapper);
            lst.forEach(o->ids.add(o.getId()));
        }
        for(Long id:ids){
            logger.info("===修复供应商【{}】日期【{}】",id,date);
            kafkaTemplate.send(KafkaTopic.STATISTICS_CAL_SUPPLIER,id.toString(),date);
            Thread.sleep(3*1000);
        }
        return ResponseBean.success();
    }

    @RequestMapping("salesman")
    public ResponseBean salesman(@RequestParam(value = "id",defaultValue = "0") Long productId,@RequestParam("date") String date) throws InterruptedException {
        List<Long> ids =new ArrayList<>();
        if(productId>0){
            ids.add(productId);
        }else{
            CollectionCache.dutyEmployees.get(20L).forEach(o->ids.add(o.getId()));
        }
        for(Long id:ids){
            logger.info("===修复业务员【{}】日期【{}】",id,date);
            kafkaTemplate.send(KafkaTopic.STATISTICS_CAL_SALESMAN,id.toString(),date);
            Thread.sleep(2*1000);
        }
        return ResponseBean.success();
    }

    @RequestMapping("destination")
    public ResponseBean destination(@RequestParam(value = "id",defaultValue = "0") Long productId,@RequestParam("date") String date) throws InterruptedException {
        List<Long> ids =new ArrayList<>();
        if(productId>0){
            ids.add(productId);
        }else{
            logisticsFeignService.getCompany().forEach(o->ids.add(o.getId()));
        }
        for(Long id:ids){
            logger.info("===修复纸厂【{}】日期【{}】",id,date);
            kafkaTemplate.send(KafkaTopic.STATISTICS_CAL_DESTINATION,id.toString(),date);
            Thread.sleep(2*1000);
        }
        return ResponseBean.success();
    }

    @RequestMapping("purchase")
    public ResponseBean purchase(@RequestParam("date") String date) throws InterruptedException {
        logger.info("===修复采购统计日期【{}】",date);
        kafkaTemplate.send(KafkaTopic.STATISTICS_CAL_PURCHASE,date,date);
        return ResponseBean.success();
    }
}
